package net.chrisrichardson.ec2deployer.cluster

import net.chrisrichardson.ec2deployer.core.EC2Server
import net.chrisrichardson.ec2deployer.core.EC2


abstract class AbstractTier implements Tier {
   long id
    EC2 ec2

    protected AbstractTier() {}
    
    protected AbstractTier(EC2 ec2) {
        this.ec2 = ec2
    }

    void waitUntilRunning(Set waitedFor) {
        waitUntilRunning(servers, waitedFor)
    }

    void waitUntilRunning(List<EC2Server> servers, Set waitedFor) {
        for (EC2Server server : servers) {
            if (!waitedFor.contains(server)) {
                waitedFor.add server
                server.waitUntilRunning()
            }
            runTierStartUpScript(server)
        }
    }

    void runTierStartUpScript(EC2Server server) {
        String prefix = getClass().simpleName
        prefix = prefix.substring(0,1).toLowerCase() + prefix.substring(1)
        runBuiltinStartupScript(server, prefix)
        runUserSpecifiedStartupScript(server, prefix)
    }

    private def runUserSpecifiedStartupScript(EC2Server server, String prefix) {
        String propertyName = prefix + ".startupScript"
        if (ec2.awsProperties.exists(propertyName)) {
            String script = ec2.awsProperties.getProperty(propertyName)
            server.executeScript script
        }
    }

    void runBuiltinStartupScript(EC2Server ec2Server, String scriptPrefix) {
        URL resource = getClass().getResource("/scripts/startup/${scriptPrefix}.sh");
        if (resource)
            ec2Server.executeScriptText (resource.text)

    }
}